﻿
@{
    ViewBag.Title = "新增油机保养";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var RunTime = ViewBag.RunTime;
    var GensetNum = ViewBag.GensetNum;
}

<link rel="stylesheet" href="~/Content/css/easyui.css" />
<script src="~/Content/script/swiper.min.js"></script>
<script src="~/Content/script/Uitls.js"></script>
<script src="~/Content/script/jquery.easyui.min.js"></script>
<script src="~/Content/script/dropload.min.js"></script>
<script src="~/Content/script/jweixin-1.4.0.js"></script>
<script src="~/Content/script/Uitls.js"></script>


<script src="~/Content/My97DatePicker/WdatePicker.js"></script>
<link rel="stylesheet" href="~/Content/My97DatePicker/calendar.css">
<style>
    .imgWh img {
        width: 100%;
        height: 150px;
    }
</style>
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<body ontouchstart>
    <div class="weui-cells weui-cells_form" style="bottom: 20px;">

        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">累计发电时长:</label></div>
            <div class="weui-cell__bd">
                <input id="GenRunPeriod" type="text" readonly="readonly" placeholder="系统计算">
            </div>
        </div>

        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">保养费用(元):</label></div>
            <div class="weui-cell__bd">
                <input id="MaintFee" class="weui-input" type="text" placeholder="输入保养费用">
            </div>
        </div>

        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">保养时间:</label></div>
            <div class="weui-cell__bd">
                <input id="maintDate" class="Wdate" type="text" placeholder="选择日期" onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',readOnly:true});">

            </div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">保养内容:</label></div>
            <div class="weui-cell__bd">
                <textarea class="weui-textarea" style="height:2.0rem" id="MaintContent" placeholder="输入保养备注内容" rows="3"></textarea>
                <div class="weui-textarea-counter"><span>0</span>/200</div>

            </div>
        </div>

        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">保养图片:</label></div>
            <div class="weui-cell__bd">
                <a id="chooseImage" href="javascript:;" class="weui-btn weui-btn_plain-default">点击拍照或选择图片</a>
            </div>
        </div>
        <div class="weui-cell">
            <ul class="aui-list-view aui-grid-view imgWh" id="addImg"></ul>
        </div>
    </div>
    <div class="weui-btn-area">
        <div id="submit" class="weui-btn weui-btn_primary">添加油机保养</div>
    </div>
</body>
<script>
    $("#GenRunPeriod").val('@RunTime');
   // alert('@GensetNum');
    var arr = []; //存放多张图片的base64码
    //微信jdk
    $.ajax({
        url: '/WeiXin/JsapiTicketSignature?fullUrl=' + window.location.href + '',
        type: 'get',
        success: function (data) {
            var result = eval(data);
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: result.appId, // 必填，公众号的唯一标识
                timestamp: result.timestamp, // 必填，生成签名的时间戳
                nonceStr: result.nonceStr, // 必填，生成签名的随机串
                signature: result.signature,// 必填，签名
                jsApiList: ["chooseImage"] // 必填，需要使用的JS接口列表
            });
        }
    });
    wx.ready(function () {

        //照相事件
        $("#chooseImage").click(function () {

            wx.chooseImage({
                count: 9, // 默认9
                sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                //方法一
                //success: function (res) {
                //    var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
                //    for (var i = 0; i < localIds.length; i++) {
                //        var localId=localIds[i];
                //        addImg(localId);
                //    }
                //    utilss.addImgV2('confirmImgNo', "confirmImgNo", "confirmAnnounce");
                //}
                //方法二
                success: function (res) {
                    var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
                   // alert("1")

                    for (var i = 0; i < localIds.length; i++) {
                        urlTobase64(localIds, i);
                    }

                }
            });;
        });

    });
    //添加事件
    $("#submit").click(function () {
        let maintPicture = "";
        if (arr.length > 0) {
            for (var i = 0; i < arr.length; i++) {
                maintPicture += arr[i];
            }
        }
        var maintData = {
            MaintId: 0,
            GensetNum: '@GensetNum',
            MaintFee: document.getElementById("MaintFee").value,
            GenRunPeriod: document.getElementById("GenRunPeriod").value,
            MaintContent: document.getElementById("MaintContent").value,
            MaintDate: $("#maintDate").val(),
            MaintPicture: maintPicture
        };
        if (maintData.MaintFee === "") {
            $.alert("保养费用不能为空!");
            return;
        }
        else {

            if (isNaN(maintData.MaintFee))
            {
                $.alert("保养费用不是数字!");
                return;
            }
        }

        if (maintData.MaintDate === "") {
            $.alert("保养时间不能为空!");
            return;
        }
        if (maintData.MaintContent === "") {
            $.alert("保养内容不能为空!");
            return;
        }

        if (maintData.MaintPicture=="") {
            $.alert("请拍照上传图片!");
            return;
        }
        $.ajax({
            //url: "/GenMain/Add",
            url: "/api/Generator/AddGenMain",
            type: 'post',
            beforeSend: function () {
                $.showLoading("提交中...");
            },
            success: function (data) {
                $.hideLoading();
                var data=data.Data
                if (data == "添加成功！") {
                    setTimeout(function () {
                        location.href = "/GenMain/Index";
                    }, 1 * 1000);
                    $.toast(data);
                }
                else {
                    $.alert(data, "提示");
                }
            },
            error: function (data) {
                $.alert("error :请求出错", "提示！");
                $.hideLoading();
            },
            data: maintData
        });
    });

    //添加图片的row
    function addImg(src){
        var li = $('<li />');
        var img = $('<img />');
         img.add
        img.attr('src', src);
        li.append(img)
        var html = "<li class='aui-col-xs-6'><img src='" + src + "' ></li>";
        $('#addImg').append(html);
    }

    //图片转base64
    function urlTobase64(url, i) {
        wx.getLocalImgData({
            localId: url[i],//图片的本地ID
            success: function (res) {
                var localData = res.localData;
                if (localData.indexOf('data:image') != 0) {
                    //判断是否有这样的头部
                    localData = 'data:image/jpeg;base64,' +  localData
                }
                localData = localData.replace(/\r|\n/g, '').replace('data:image/jgp', 'data:image/jpeg'); // 此处的localData 就是你所需要的base64位
                addImg(localData);
                arr.push(localData);
            }
        })
    }
</script>